﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Mr.LinqToWeibo
{
    public class QuerableWeiboData<TEntity>
        : IQueryable<TEntity>
    {
        public QuerableWeiboData()
        {
            this.Provider = new WeiboDataQueryProvider();
            this.Expression = Expression.Constant(this);
        }

        internal QuerableWeiboData(IQueryProvider provider, Expression expression)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }

            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            this.Provider = provider;
            this.Expression = expression;
        }

        public Type ElementType
        {
            get { return typeof(TEntity); }
        }

        public Expression Expression { get; private set; }

        public IQueryProvider Provider { get; private set; }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (this.Provider.Execute<IEnumerable>(this.Expression)).GetEnumerator();
        }

        public IEnumerator<TEntity> GetEnumerator()
        {
            return (Provider.Execute<IEnumerable<TEntity>>(Expression)).GetEnumerator();
        }
    }
}
